/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
namespace GearBlog.Web.Models
{
	using System;
	using Castle.ActiveRecord;
	using Castle.ActiveRecord.Validators;
	using Castle.Components.Validator;
	using Castle.MonoRail.Framework;
	using NHibernate.Expression;

	[ActiveRecord]
	public class UserInviteTicket:ExtendedActiveRecordBase<UserInviteTicket>
	{
		private Guid _inviteKey;
		private string _fullName;
		private string _email;


		public UserInviteTicket()
		{
			_inviteKey = Guid.NewGuid();
		}

		[Property(NotNull=true),ValidateCompositeIsUnique(new string[]{"Email"}, "Email")]
		public string Email
		{
			get { return _email; }
			set { _email = value; }
		}

		[Property(NotNull=true),ValidateCompositeIsUnique(new string[] {"InviteKey"},"Invite Key" )]
		public Guid InviteKey
		{
			get { return _inviteKey; }
			set { _inviteKey = value; }
		}

		[Property(NotNull = true),ValidateNonEmpty]
		public string FullName
		{
			get { return _fullName; }
			set { _fullName = value; }
		}

		public void SendInvitation(Controller controller)
		{
			Setting setting = Setting.FindOne();
			controller.PropertyBag["UserInviteTicket"] = this;
			controller.PropertyBag["OwnerEmail"] = setting.OwnerEmail;
			controller.PropertyBag["Title"] = setting.Title;
			controller.RenderEmailAndSend("UserInvite");

		}

		public static UserInviteTicket FindByInviteKey(Guid inviteKey)
		{
			return FindOne(new EqExpression("InviteKey", inviteKey));
		}

		public static UserInviteTicket FindByEmail(string email)
		{
			return FindOne(new EqExpression("Email", email));

		}
	}
}
